<template>
  <div class="login">
    <div class="box">
      <el-form label-width="60px" :model="loginForm" :rules="loginRules" ref="loginFormRef" @keyup.enter.native="submitForm">
        <h3>后台管理系统</h3>
        <el-form-item label="用户名">
          <el-input v-model="loginForm.username" prefix-icon="el-icon-user-solid" clearable placeholder="请输入用户名"></el-input>
        </el-form-item>
        <el-form-item label="密码">
          <el-input placeholder="请输入密码" v-model="loginForm.password" prefix-icon="el-icon-lock" show-password></el-input>
        </el-form-item>
        <el-form-item style="margin: 30px 0 0 0px">
          <el-button @click="reset">重置</el-button>
          <el-button style="margin-left: 30px" type="primary" size="medium" @click="submitForm">登录</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
export default {
  name: 'FirstProjectLogin',
  data() {
    return {
      loginForm: {
        username: '',
        password: ''
      },
      loginRules: {
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' },
          { min: 1, max: 8, message: '长度在 1 到 8 个字符', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          { min: 3, max: 15, message: '长度在 3 到 15 个字符', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    submitForm() {
      this.$refs.loginFormRef.validate(async valid => {
        if (valid) {
          try {
            const { data: res } = await this.$http.post('/user/login', this.loginForm)
            if (res.code !== 0) {
              return this.$message.error('账号或密码错误')
            }
            this.$store.state.username = this.loginForm.username
            this.$message.success('登录成功')
            window.sessionStorage.setItem('username', this.loginForm.username)
            this.loginForm.username = ''
            this.loginForm.password = ''
            window.sessionStorage.setItem('token', res.token)
            this.$router.push('/')
          } catch (err) {
            console.log(err)
          }
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },
    reset() {
      this.loginForm.username = ''
      this.loginForm.password = ''
    }
  }
}
</script>

<style lang="scss" scoped>
.login {
  width: 100%;
  height: 100%;
  background: url('../../assets/bjt.jpeg');
  background-size: cover;
  display: flex;
  justify-content: center;
  align-items: center;
  flex-flow: column;
  color: #fff;
}

.box {
  width: 500px;
  height: 400px;
  background-color: rgba(255, 255, 255, 0.8);
  display: flex;
  justify-content: center;
  align-items: center;
  flex-flow: column;
  border-radius: 4px;
  box-shadow: 0 0 8px 8px #ccc;
}

h3 {
  margin-bottom: 40px;
  font-size: 36px;
  letter-spacing: 2px;
  color: #888;
  text-align: center;
}
</style>
